package com.wanhe.qrcode;

import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.widget.Toast;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.util.Hashtable;

public class QRCodeUtil {

	public static Bitmap creatQRCode(String content, int size) {
		try {
			Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
			hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
			hints.put(EncodeHintType.MARGIN, 1);
			hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
			BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hints);
			int[] pixels = new int[size * size];
			for (int y = 0; y < size; y++) {

				for (int x = 0; x < size; x++) {

					if (bitMatrix.get(x, y)) {
						pixels[y * size + x] = 0xff000000;
					} else {
						pixels[y * size + x] = 0xffffffff;
					}
				}
			}
			Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
			bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
			return bitmap;
		} catch (WriterException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Bitmap creatAvatar(Context context, String userName, int size) {
		Bitmap avatar = null;
		try {
			// avatar = AvatarLoader.getInstance().getDeskCachedBmp(userName);
			if (avatar == null) {
				avatar = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_avatar_defaul);

			}
			Matrix mMatrix = new Matrix();
			float width = avatar.getWidth();
			float height = avatar.getHeight();
			mMatrix.setScale(size / width, size / height);
			return Bitmap.createBitmap(avatar, 0, 0, (int) width, (int) height, mMatrix, true);

		} catch (Exception e) {
			e.printStackTrace();
			avatar = BitmapFactory.decodeResource(context.getResources(),
					R.drawable.ic_avatar_defaul);
			Matrix mMatrix = new Matrix();
			float width = avatar.getWidth();
			float height = avatar.getHeight();
			mMatrix.setScale(size / width, size / height);
			return Bitmap.createBitmap(avatar, 0, 0, (int) width, (int) height, mMatrix, true);
		}

	}

	//	public static Bitmap getAccountAvatar() {
	//
	//		DisplayImageOptions options = ImageOptions.getInstance().getAvatarImgOptionByCache();
	//		String path = Parent.getInstance().getHeadUrl();
	//		Bitmap bmp_avatar = ImageLoader.getInstance().loadImageSync(path, options);
	//		return bmp_avatar;
	//	}

	public static void createQRCodeWithAvatar(Bitmap qrCode, Bitmap avatar, int avatarSize) {
		// 二维码图片的大小
		int qr_W = qrCode.getWidth();
		int qr_H = qrCode.getHeight();

		// 设置头像要显示的位置，即居中显示
		int left = (qr_W - avatarSize) / 2;
		int top = (qr_H - avatarSize) / 2;
		int right = left + avatarSize;
		int bottom = top + avatarSize;
		Rect rect1 = new Rect(left, top, right, bottom);

		// 取得qr二维码图片上的画笔，即要在二维码图片上绘制我们的头像
		Canvas canvas = new Canvas(qrCode);

		// 设置我们要绘制的范围大小，也就是头像的大小范围
		Rect rect2 = new Rect(0, 0, avatarSize, avatarSize);
		// 开始绘制
		canvas.drawBitmap(avatar, rect2, rect1, null);
	}

	public static Bitmap creatQrCodeBtmp(Context context) {
		Bitmap btmp_qrCode = null;
		String strQR = "jdflajfhahahhahaldjflajflasdjkflsdfkj";
		int qrSize = (int) dp2px(context, 250);

		btmp_qrCode = creatQRCode(strQR, qrSize);
		//		int avatarSize = (int) TelephoneUtil.dp2px(45);
		//		Bitmap avatar = getAccountAvatar();

		// avatar = ImageUtility.toRoundBitmap(avatar);

		//		createQRCodeWithAvatar(btmp_qrCode, avatar, avatarSize);

		return btmp_qrCode;
	}

	public static void dealQRString(Context ctx, String qrString) {
//		QRShowTextActivity.startQRShowTextActivity(ctx, qrString);

		if (TextUtils.isEmpty(qrString)) {
			Toast.makeText(ctx, R.string.addfriend_txt_QRcode_wrong, Toast.LENGTH_SHORT).show();
			return;
		}

	}

	public static String getDeviceIDForServer(Context mContext) {
		// step1: get IMEI
		String strID = getIMEI(mContext);

		// step2: if IMEI null, get MAC address
		if (TextUtils.isEmpty(strID)) {
			strID = getMAC(mContext);
		}

		// step3: if MAC address null, get Android ID
		if (TextUtils.isEmpty(strID)) {
			strID = getAndroidID(mContext);
		}

		return strID;
	}
	public static String getIMEI(Context mContext) {
		try {
			TelephonyManager tm = (TelephonyManager) mContext.getSystemService(
					Context.TELEPHONY_SERVICE);
			return tm.getDeviceId();
		} catch (Exception e) {
			return "";
		}
	}

	public static String getMAC(Context mContext) {
		WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = wifi.getConnectionInfo();
		return info.getMacAddress();
	}

	public static String getAndroidID(Context mContext) {
		return Settings.System
				.getString(mContext.getContentResolver(), Settings.Secure.ANDROID_ID);
	}

	/**
	 * 判断是否有摄像头
	 *
	 * @return
	 */
	public static boolean hasCamera(Context context) {
		if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
			return true;
		} else {
			return false;
		}
	}

	public static int dp2px(Context context, float dpValue) {
		return (int) (dpValue * getDensity(context) + 0.5f);
	}

	public static float getDensity(Context context) {
		DisplayMetrics metrics = getDisplayMetrics(context);
		return metrics.density;
	}

	public static DisplayMetrics getDisplayMetrics(Context context) {
		return context.getResources().getDisplayMetrics();
	}

	public static Bitmap RotateBitmap(Bitmap source, float angle) {

		Matrix matrix = new Matrix();
		matrix.postRotate(angle);
		return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
	}

	public static boolean hasGingerBread() {
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD;
	}


}
